/*******************************************************
> Copyright (C) 2024 ==IOT== All rights reserved.
> File Name: revise.c
> Author: sy
> Website:www.sy.org
> Created Time: 2024年09月23日 星期一 18时01分13秒
***************************************************/

//#include "login.h"
#include "revise.h"
//#include "enroll.c"

// 设备管理
void ModifyDevice()
{
    struct manage_device dev;
    memset(&dev, 0, sizeof(dev));

    printf("...add.................delete\n");
    scanf("%s", dev.dir);

    printf("输入设备名称和设备id,\n");
    scanf("%s%s", dev.name, dev.id);

    // 设置类型和指令
    strcpy(dev.type, "MANAGER");
    strcpy(dev.directives, "ModifyDevice");

    char *jsonStr = input_cJSON_modify_device(dev);
    if (jsonStr == NULL)
    {
        fprintf(stderr, "生成JSON失败\n");
        return;
    }

    int sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if (sockfd == -1)
    {
        perror("socket");
        free(jsonStr);
        return;
    }

    struct sockaddr_in addr;
    memset(&addr, 0, sizeof(addr));
    addr.sin_family = AF_INET;
    addr.sin_port = htons(PORT);
    addr.sin_addr.s_addr = inet_addr(IP);

    if (connect(sockfd, (struct sockaddr *)&addr, sizeof(addr)) == -1)
    {
        perror("connect");
        close(sockfd);
        free(jsonStr);
        return;
    }

    char *response = sendto_and_receive(jsonStr, sockfd);
    free(jsonStr);
    close(sockfd);

    if (response)
    {
        output_cJSON_reg(response);
        free(response);
    }
    else
    {
        printf("未收到响应\n");
    }
}

char *input_cJSON_modify_device(struct manage_device dev)
{
    cJSON *json = cJSON_CreateObject();
    if (!json)
    {
        printf("Failed to create JSON object\n");
        return NULL;
    }

    cJSON_AddItemToObject(json, "type", cJSON_CreateString(dev.type));
    cJSON_AddItemToObject(json, "directives", cJSON_CreateString(dev.directives));
    cJSON_AddItemToObject(json, "dir", cJSON_CreateString(dev.dir));
    cJSON_AddItemToObject(json, "name", cJSON_CreateString(dev.name));
    cJSON_AddItemToObject(json, "id", cJSON_CreateString(dev.id));

    char *dest = cJSON_Print(json);
    cJSON_Delete(json);
    return dest;
}

void output_cJSON_modify_device(char *dest)
{
    cJSON *json_response = cJSON_Parse(dest);
    if (!json_response)
    {
        printf("Failed to parse JSON response\n");
        free(dest); // 确保释放内存
        return;
    }

    cJSON *node = cJSON_GetObjectItem(json_response, "directives");
    if (node && node->type == cJSON_String)
    {
        if (strcmp(node->valuestring, "ModifyDevice") == 0)
        {
            node = cJSON_GetObjectItem(json_response, "ause");
            if (node && node->type == cJSON_String)
            {
                if (strcmp(node->valuestring, "success") == 0)
                {
                    printf("成功\n");
                }
                else if (strcmp(node->valuestring, "Default") == 0)
                {
                    printf("失败\n");
                    node = cJSON_GetObjectItem(json_response, "info");
                    if (node && node->type == cJSON_String)
                    {
                        printf("info: %s\n", node->valuestring);
                    }
                }
            }
        }
    }

    cJSON_Delete(json_response);
    free(dest); // 确保释放内存
}

// 用户管理
void ModifyStaff()
{
    struct manage_staff sta;
    memset(&sta, 0, sizeof(sta));

    printf("...add.................delete\n");
    scanf("%s", sta.dir);

    if (strcmp(sta.dir, "add") == 0)
    {
        Register(); // 添加用户的逻辑
    }
    else if (strcmp(sta.dir, "delete") == 0)
    {
        printf("输入用户名和用户id,\n");
        scanf("%s%s", sta.name, sta.id);

        strcpy(sta.type, "MANAGER");
        strcpy(sta.directives, "ModifyStaff");

        char *jsonStr = input_cJSON_modify_staff(sta);
        if (jsonStr == NULL)
        {
            fprintf(stderr, "生成JSON失败\n");
            return;
        }

        int sockfd = socket(AF_INET, SOCK_STREAM, 0);
        if (sockfd == -1)
        {
            perror("socket");
            free(jsonStr);
            return;
        }

        struct sockaddr_in addr;
        memset(&addr, 0, sizeof(addr));
        addr.sin_family = AF_INET;
        addr.sin_port = htons(PORT);
        addr.sin_addr.s_addr = inet_addr(IP);

        if (connect(sockfd, (struct sockaddr *)&addr, sizeof(addr)) == -1)
        {
            perror("connect");
            close(sockfd);
            free(jsonStr);
            return;
        }

        char *response = sendto_and_receive(jsonStr, sockfd);
        free(jsonStr);
        close(sockfd);

        if (response)
        {
            output_cJSON_reg(response);
            free(response);
        }
        else
        {
            printf("未收到响应\n");
        }
    }
}
char *input_cJSON_modify_staff(struct manage_staff sta)
{
    cJSON *json = cJSON_CreateObject();
    if (!json)
    {
        printf("Failed to create JSON object\n");
        return NULL;
    }

    cJSON_AddItemToObject(json, "type", cJSON_CreateString(sta.type));
    cJSON_AddItemToObject(json, "directives", cJSON_CreateString(sta.directives));
    cJSON_AddItemToObject(json, "dir", cJSON_CreateString(sta.dir));
    cJSON_AddItemToObject(json, "name", cJSON_CreateString(sta.name));
    cJSON_AddItemToObject(json, "id", cJSON_CreateString(sta.id));

    char *dest = cJSON_Print(json);
    cJSON_Delete(json);
    return dest;
}

void output_cJSON_modify_staff(char *dest)
{
    cJSON *json_response = cJSON_Parse(dest);
    if (!json_response)
    {
        printf("Failed to parse JSON response\n");
        free(dest); // 确保释放内存
        return;
    }

    cJSON *node = cJSON_GetObjectItem(json_response, "directives");
    if (node && node->type == cJSON_String)
    {
        if (strcmp(node->valuestring, "ModifyStaff") == 0)
        {
            node = cJSON_GetObjectItem(json_response, "ause");
            if (node && node->type == cJSON_String)
            {
                if (strcmp(node->valuestring, "success") == 0)
                {
                    printf("modify staff successful\n");
                }
                else if (strcmp(node->valuestring, "Default") == 0)
                {
                    printf("modify staff failed\n");
                    node = cJSON_GetObjectItem(json_response, "info");
                    if (node && node->type == cJSON_String)
                    {
                        printf("info: %s\n", node->valuestring);
                    }
                }
            }
        }
    }

    cJSON_Delete(json_response);
    free(dest); // 确保释放内存
}

// 修改密码
void ChangePassword()
{
    struct manager man;
    memset(&man, 0, sizeof(man));

    printf("请输入账户id:旧密码:新密码\n");
    scanf("%s%s%s", man.id, man.password, man.new_password);
    strcpy(man.type, "MANAGER");
    strcpy(man.directives, "ChangePassword");

    char *jsonStr = input_cJSON_change_passwd(man);
    if (jsonStr == NULL)
    {
        printf("生成JSON失败\n");
        return;
    }
    
    int sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if (sockfd == -1)
    {
        perror("socket");
        free(jsonStr);
        return;
    }

    struct sockaddr_in addr;
    memset(&addr, 0, sizeof(addr));
    addr.sin_family = AF_INET;
    addr.sin_port = htons(PORT);
    addr.sin_addr.s_addr = inet_addr(IP);

    if (connect(sockfd, (struct sockaddr *)&addr, sizeof(addr)) == -1)
    {
        perror("connect");
        close(sockfd);
        free(jsonStr);
        return;
    }

    char *response = sendto_and_receive(jsonStr, sockfd);
    free(jsonStr);
    close(sockfd);

    if (response)
    {
        output_cJSON_reg(response);
        free(response);
    }
    else
    {
        printf("未收到响应\n");
    }
}

char *input_cJSON_change_passwd(struct manager man)
{
    cJSON *json = cJSON_CreateObject();
    if (!json)
    {
        printf("Failed to create JSON object\n");
        return NULL;
    }

    cJSON_AddItemToObject(json, "type", cJSON_CreateString(man.type));
    cJSON_AddItemToObject(json, "id", cJSON_CreateString(man.id));
    cJSON_AddItemToObject(json, "password", cJSON_CreateString(man.password));
    cJSON_AddItemToObject(json, "new_password", cJSON_CreateString(man.new_password));
    cJSON_AddItemToObject(json, "directives", cJSON_CreateString(man.directives));

    char *dest = cJSON_Print(json);
    cJSON_Delete(json);
    return dest;
}

void output_cJSON_change_passwd(char *dest)
{
    cJSON *json_response = cJSON_Parse(dest);
    if (!json_response)
    {
        printf("Failed to parse JSON response\n");
        return;
    }

    cJSON *node = cJSON_GetObjectItem(json_response, "directives");
    if (node && node->type == cJSON_String)
    {
        if (strcmp(node->valuestring, "ChangePassword") == 0)
        {
            node = cJSON_GetObjectItem(json_response, "ause");
            if (strcmp(node->valuestring, "success") == 0)
            {
                printf("change password successful\n");
            }
            else if (strcmp(node->valuestring, "fail") == 0)
            {
                printf("change password failed\n");
                node = cJSON_GetObjectItem(json_response, "info");
                if (node && node->type == cJSON_String)
                {
                    printf("info: %s\n", node->valuestring);
                }
            }
        }
    }

    cJSON_Delete(json_response);
    free(dest);
}